package com.ingenio.siot.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ingenio.siot.adapter.CriterioAdapter;
import com.ingenio.siot.adapter.EstadosTransitoAdapter;
import com.ingenio.siot.entity.Criterio;
import com.ingenio.siot.entity.EstadoTransito;

/**
 * Servlet implementation class DevolverParametroServlet
 */
@WebServlet("/DevolverEstadoTransito")
public class DevolverEstadoTransitoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DevolverEstadoTransitoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			JSONArray json = new JSONArray();
			CriterioAdapter ca = new CriterioAdapter();
			Criterio c = null;
			if(request.getParameter("idCriterio") != ""){
				Integer idCriterio = Integer.parseInt(request.getParameter("idCriterio").trim());
				c = ca.devolverCriterio(idCriterio);
			}else{
				c = ca.devolverCriterioPublico();
			}
		    EstadosTransitoAdapter eta = new EstadosTransitoAdapter();
			List<EstadoTransito> estadosTransito = eta.devolverEstadosTrantido(c);
			if (estadosTransito == null){
				JSONObject linea = new JSONObject();
				linea.put("error", "No se pudo recuperar transito"); 
				response.setContentType("application/json");
				response.getWriter().write(linea.toString());
			}else{
				for (EstadoTransito et: estadosTransito){
					JSONObject linea = new JSONObject();
					JSONObject linicio = new JSONObject();
					JSONObject lfin = new JSONObject();
					linicio.put("lat", et.getTransito().getLinea().getAlturaInicioDibujo().getLatitud());
					linicio.put("lon", et.getTransito().getLinea().getAlturaInicioDibujo().getLongitud());
					lfin.put("lat", et.getTransito().getLinea().getAlturaFinDibujo().getLatitud());
					lfin.put("lon", et.getTransito().getLinea().getAlturaFinDibujo().getLongitud());
					linea.put("inicio", linicio);
					linea.put("fin", lfin);
					linea.put("color", et.getEstado().getColor());
					linea.put("idlinea", et.getTransito().getLinea().getIdLinea());
					json.put(linea);
				}
			}
			response.setContentType("application/json");
			response.getWriter().write(json.toString());
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}
